package com.ws.eduservice.controller;


import com.ws.commonutils.R;
import com.ws.eduservice.entity.EduChapter;
import com.ws.eduservice.entity.chapter.ChapterVo;
import com.ws.eduservice.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ws
 * @since 2024-12-04
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;

    @GetMapping("getAddChaper/{couserId}")
    public R getAddChaper(@PathVariable String couserId) {
        List<ChapterVo> chapterVoList=chapterService.getAddChaperList(couserId);
        return R.ok().data("chapterVoList",chapterVoList);
    }

    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        boolean save = chapterService.save(eduChapter);
        if(!save){
            return R.error().message("添加章节失败");
        }
        return R.ok();

    }
    @GetMapping("getChapter/{id}")
    public R getChapter(@PathVariable String id) {
        EduChapter eduChapter = chapterService.getById(id);
        return R.ok().data("chapter",eduChapter);
    }

    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        boolean isUpdate = chapterService.updateById(eduChapter);
        if(!isUpdate){
            return R.error().message("更新章节失败");
        }
        return R.ok();
    }

    @DeleteMapping("deleteChapter/{id}")
    public R deleteChapter(@PathVariable String id) {
        Boolean res=chapterService.deleteById(id);
        if (!res){
            return R.error().message("删除章节失败");
        }
        return R.ok();
    }
}

